// 统一API请求方法
const API = {
    index: "/index",
    forecastPage: "/forecast",
    optimizePage: "/optimize",
    login: "/api/user/login",
    register: "/api/user/register",
    forecast: "/api/energy/forecast",
    optimize: "/api/schedule/optimize",
};

function callAPI(url, data, callback) {
    console.log("here");
    $.ajax({
        type: "POST",
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('token')}`
        },
        url: url,
        contentType: "application/json",
        processData: false,
        data: JSON.stringify(data),
        success: callback,
        error: handleAPIError
    });
}

function callAPIWithFile(url, data, callback) {
    $.ajax({
        type: "POST",
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('token')}`
        },
        url: url,
        data: data,
        processData: false,
        contentType: false,
        success: callback,
        error: handleAPIError
    });
}

function handleAPIError(jqXHR, textStatus, errorThrown) {
    console.error("API 请求出错:", textStatus, errorThrown);
    // 可以在此添加更多的错误处理逻辑，比如显示错误消息给用户
}
